import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

//生产者
class producer implements Runnable {
    Queue<Integer> queue;//创建队列对象
    String name;
    int maxsize;
    int i = 0;//模拟生产整型数据

    public producer(Queue<Integer> queue, String name, int maxsize) {
        this.queue = queue;
        this.name = name;
        this.maxsize = maxsize;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (queue) {
                while (queue.size() == maxsize) {//自旋锁
                    /*
                    自旋锁（spinlock）：是指当一个线程在获取锁的时候，如果锁已经被其它线程获取，那
                    么该线程将循环等待，然后不断的判断锁是否能够被成功获取，直到获取到锁才会退出循环。
                     */
                    System.out.println("队列是满的，生产者[" + name + "]在等待消费者从仓库里拿东西");
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("[" + name + "]正在生产值" + i);
                queue.offer(i++);//模拟消费者向仓库中添加东西，添加一个元素并返回true,如果队列已满，则返回false
                queue.notifyAll();
                try {
                    Thread.sleep(new Random().nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

//消费者
class consumer implements Runnable {
    private Queue<Integer> queue;
    private String name;
    private int maxsize;

    public consumer(Queue<Integer> queue, String name, int maxsize) {
        this.queue = queue;
        this.name = name;
        this.maxsize = maxsize;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (queue) {
                while (queue.isEmpty()) {
                    System.out.println("队列是空的，消费者[" + name + "]在等待生产者生产东西");
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                int x = queue.poll();//模拟消费者拿仓库东西,移除并返问队列头部的元素,如果队列为空,则返回null
                System.out.println("[" + name + "]正在消费东西：" + x);
                queue.notifyAll();
                try {
                    Thread.sleep(new Random().nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class Storage {
    private static final int CAPACITY = 5;
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<Integer>();
        Thread p1 = new Thread(new producer(queue, "生产者一", CAPACITY));
        Thread p2 = new Thread(new producer(queue, "生产者二", CAPACITY));
        Thread c1 = new Thread(new consumer(queue, "消费者一", CAPACITY));
        Thread c2 = new Thread(new consumer(queue, "消费者二", CAPACITY));
        Thread c3 = new Thread(new consumer(queue, "消费者三", CAPACITY));
        p1.start();
        p2.start();
        c1.start();
        c2.start();
        c3.start();
    }
}

